﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmMembersEditor : Form
    {
        private int _MemberClass;
        msgMembershipInfo[] _ActualMembers;
        msgMembershipInfo[] _FindResults;

        public msgMembershipInfo[] ActualMembers
        {
            get
            {
                return _ActualMembers;
            }
        }



        public frmMembersEditor(msgMembershipInfo[] ActualMembers, int MemberClass, string MemberName)
        {
            InitializeComponent();
            txtMemberName.Text = MemberName;
            lvwMembers.View = View.Details;
            _MemberClass = MemberClass;
            _ActualMembers = ActualMembers;
            foreach (msgMembershipInfo member in ActualMembers)
            {
                ListViewItem it = lvwMembers.Items.Add(member.MemberID.ToString());
                it.SubItems.Add(member.Description);
            }
        }

        private void frmMembersEditor_Load(object sender, EventArgs e)
        {
        
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;
            try
            {
                _FindResults = MembersClasses.MemberBase.FindMembershipInfo(_MemberClass, txtFindPattern.Text);
                lvwSearchResults.View = View.Details;
                lvwSearchResults.Items.Clear();
                if (_FindResults != null)
                {
                    foreach (msgMembershipInfo member in _FindResults)
                    {
                        ListViewItem it = lvwSearchResults.Items.Add(member.MemberID.ToString());
                        it.SubItems.Add(member.Description);
                    }
                }
            }
            catch
            {
                MessageBox.Show(Properties.Resources.UnableToFind);
            }
            finally 
            {
                this.Cursor = Cursors.Default;
                this.Enabled = true;
            }
        }

        private void lvwSearchResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwSearchResults.SelectedItems.Count == 1)
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lvwSearchResults.SelectedItems.Count == 1)
            {
                int itemid = Int32.Parse(lvwSearchResults.SelectedItems[0].Text);
                bool found = false;
                foreach (msgMembershipInfo item in _ActualMembers)
                {
                    if (item.MemberID == itemid)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    msgMembershipInfo SelItem = null;
                    foreach (msgMembershipInfo member in _FindResults)
                    {
                        if (member.MemberID == itemid)
                        {
                            ListViewItem it = lvwMembers.Items.Add(member.MemberID.ToString());
                            it.SubItems.Add(member.Description);
                            SelItem = member;
                            break;
                        }
                    }                    
                    msgMembershipInfo[] tmp = new msgMembershipInfo[_ActualMembers.Length + 1];
                    _ActualMembers.CopyTo(tmp, 0);
                    tmp[tmp.Length-1] = SelItem;
                    _ActualMembers = tmp;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvwMembers.SelectedItems.Count == 1)
            {
                int itemid = Int32.Parse(lvwMembers.SelectedItems[0].Text);
                int idx = 0;
                foreach (msgMembershipInfo item in _ActualMembers)
                {
                    if (item.MemberID == itemid)
                    {
                        lvwMembers.SelectedItems[0].Remove();
                        msgMembershipInfo[] tmp = new msgMembershipInfo[_ActualMembers.Length -1];
                        for(int i = 0; i<idx; i++)
                            tmp[i] = _ActualMembers[i];
                        for (int i = idx+1; i < _ActualMembers.Length; i++)
                            tmp[i-1] = _ActualMembers[i];
                        _ActualMembers = tmp;
                        break;
                    }
                    idx++;
                }
            }
        }

        private void lvwMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwMembers.SelectedItems.Count == 1)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        


    }
}